package com.net.oil.push;

import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.push.bean.NewsDetailBean;
import com.net.oil.push.bean.PushBean;
import com.net.oil.push.bean.TechnologySupportDetail;
import com.net.oil.push.contract.PushHandleContract;
import com.net.oil.push.presenter.PushHandlePresenter;
import com.net.oil.util.StringUtil;
import com.net.oil.webview.WebViewActivity;

public class PushHandleActivity extends BaseActivity<PushHandlePresenter> implements PushHandleContract.View{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new PushHandlePresenter(this);
        initDate();
    }

    private void initDate() {
        String json = getIntent().getStringExtra(Constant.INTENT_PUSH_JSON);
        if (!StringUtil.isEmpty(json)){
            notificationHandle(json);
        }
    }

    /**
     * notificationType：1：(展示型)，2：(活动类型)，3：(新闻类型)，4：(技术支援类型)， 5（纯文本推送，点击不做跳转处理）
     * @param json  PushBean json
     *
     *
     *
     *              notificationBusiId：
    (展示型) (对notificationBusiId不处理直接跳转到我的爱车)；
    (活动类型) (根据活动notificationBusiId请求活动详情数据并跳转到活动详情)；
    (新闻类型) (根据新闻notificationBusiId请求新闻详情数据并跳转到新闻详情)；
    (技术支援类型) (根据技术支援文章notificationBusiId请求支援文章详情数据并跳转到技术支援文章详情)；

     *
     */
    private void notificationHandle(String json){
        Gson gson = new Gson();
        PushBean pushBean = gson.fromJson(json, PushBean.class);

        switch (pushBean.getNotificationType()){
            case "1":
                finish();
                break;
            case "2":
                finish();
                break;
            case "3":
                showProgressDialog();
                presenter.getNewsDetail(Integer.parseInt(pushBean.getNotificationBusiId()));
                finish();
                break;
            case "4":
                showProgressDialog();
                presenter.getTechnologySupportDetail(Integer.parseInt(pushBean.getNotificationBusiId()));
                finish();
                break;
            case "5":
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 获取技术支持文章成功
     * @param detail    技术支援文章
     */
    @Override
    public void getTechnologySupportDetailSuccess(TechnologySupportDetail detail) {
        dismissProgressDialog();
        if (detail != null && !StringUtil.isEmpty(detail.getOpenUrl())){
            Intent intent = new Intent(this, WebViewActivity.class);
            intent.putExtra(Constant.INTENT_TO_WEBVIEW_URL_KEY, detail.getOpenUrl());
            startActivity(intent);
            finish();
        }
        // TODO: 2018/4/22 0022 openurl == null
    }

    /**
     * 获取技术支持文章失败
     */
    @Override
    public void getTechnologySupportDetailFailed() {
        dismissProgressDialog();
        finish();
    }

    /**
     * 获取新闻详情成功
     * @param newsDetailBean    新闻详情
     */
    @Override
    public void getNewsDetailSuccess(NewsDetailBean newsDetailBean) {
        dismissProgressDialog();
        if (newsDetailBean != null){
            if (!StringUtil.isEmpty(newsDetailBean.getOpenUrl())){
                Intent intent = new Intent(this, WebViewActivity.class);
                intent.putExtra(Constant.INTENT_TO_WEBVIEW_URL_KEY, newsDetailBean.getOpenUrl());
                startActivity(intent);
            }
            // TODO: 2018/4/22 0022 openurl == null
        }
        finish();
    }

    /**
     * 获取新闻详情失败
     */
    @Override
    public void getNewsDetailFailed() {
        dismissProgressDialog();
        finish();
    }
}
